//
//  CZComposeToolBar.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
enum CZComposeToolBarButtonType: Int {
	//图片
	case Picture = 0
	//图片
	case Mention = 1
	//图片
	case Trend = 2

	//图片
	case Emoticun = 3
	//图片
	case Add = 4
	
	
}
@available(iOS 9.0, *)
class CZComposeToolBar: UIStackView {
	//设置回调闭包
	var didSeletedButonClosure: ((type: CZComposeToolBarButtonType) -> ())?
	//表情按钮
	var emoticonButton: UIButton?
	override init(frame: CGRect) {
		super.init(frame: frame)
		setUpUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

	private func setUpUI() {
//	backgroundColor = UIColor.redColor()
		axis = .Horizontal
		distribution = .FillEqually
		addChidButton("compose_toolbar_picture", type: .Picture)
	
		addChidButton("compose_mentionbutton_background", type: .Mention)
		addChidButton("compose_trendbutton_background", type: .Trend)
		emoticonButton = addChidButton("compose_emoticonbutton_background", type: .Emoticun)
		addChidButton("compose_add_background", type: .Add)
		
		
	}
	
	
	///添加子控件的方法
	private func addChidButton(imageName: String, type: CZComposeToolBarButtonType) -> UIButton {
	let button = UIButton()
		button.tag = type.rawValue
		button.addTarget(self, action: #selector(CZComposeToolBar.buttonClick(_:)), forControlEvents: .TouchUpInside)
	button.setImage(UIImage(named: imageName), forState: .Normal)
	button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
		
		//设置背景图片
		button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
//		去掉高亮效果
		button.adjustsImageWhenHighlighted = false
		
		
		addArrangedSubview(button)
		return button
	}
	// MARK: ------点击事件------
	@objc private func buttonClick(button: UIButton) {
		let type = CZComposeToolBarButtonType(rawValue: button.tag)!
		didSeletedButonClosure?(type: type)
		
	
	}
	
	func showEmoticonIcon(isEmoticon: Bool) {
		//标签键盘显示 keyboard
		var icon = "compose_emoticonbutton_background"
		if isEmoticon {
			icon = "compose_keyboardbutton_background"
		}
			emoticonButton?.setImage(UIImage(named: icon), forState: .Normal)
			emoticonButton?.setImage(UIImage(named: "\(icon)_highlighted"), forState: .Highlighted)
		}
}
